package pl.edu.agh.portal.application.portlets;

import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import pl.edu.agh.portal.application.utils.PortletsUtils;
import pl.edu.agh.thesis.webservices.employee.client.EmployeeUtils;
import pl.edu.agh.thesis.webservices.task.client.TaskManagerStub;
import pl.edu.agh.thesis.webservices.task.client.TaskUtils;

public class TaskCreatePortlet extends ThesisGenericPortlet {

	private static final String NORMAL_VIEW = "/task_create_portlet.jsp";
	
	private PortletRequestDispatcher normalView;
	
	private Map<String, String> errorList = new HashMap<String, String>();
	private TaskManagerStub.Task temporaryTask;	
	
	private int reporterId;
	
	public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
		
		reporterId = EmployeeUtils.validate(PortletsUtils.temp_session);
		
		if (errorList.size() == 0) {		
			temporaryTask = new TaskManagerStub.Task();
		}
		request.setAttribute(PortletsUtils.ATTRIBUTE_ERROR_MESSAGES, errorList);
		request.setAttribute(PortletsUtils.ATTRIBUTE_TASK_FOR_CREATION, temporaryTask);
		normalView.include(request, response);
	}
	
	@Override
	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {		
		
		String value = request.getParameter(PortletsUtils.TASK_CREATE_KEY);		
		if (value.equals(PortletsUtils.BUTTON_CANCEL)) {
			errorList = new HashMap<String, String>();
			response.sendRedirect("/portal/portal/project_details.psml");
		} else if (value.equals(PortletsUtils.BUTTON_CREATE)) {
			validate(request);
			if (errorList.size() == 0) {				
				Calendar cal = Calendar.getInstance();
				cal.setTime(cal.getTime());
				
				String assigneeId = request.getParameter(PortletsUtils.PARAMETER_TASK_ASSIGNEE);
				if (assigneeId == null) {
					assigneeId = String.valueOf(reporterId);
				}
				
				TaskUtils.create(PortletsUtils.temp_session, temporaryTask.getName(), Integer.parseInt(assigneeId), reporterId, temporaryTask.getPriority(), (String)PortletsUtils.vault.get("projectId"), cal, temporaryTask.getDescription(), temporaryTask.getStatus());				
				response.sendRedirect("/portal/portal/project_details.psml");
			}			
		} else {
			response.setPortletMode(PortletMode.VIEW);
		}
	}	
	
	public void init(PortletConfig config) throws PortletException {
		super.init(config);
		normalView = config.getPortletContext().getRequestDispatcher(NORMAL_VIEW);
	}
	
	public void destroy() {
		normalView = null;
		super.destroy();
	}

	private void validate(ActionRequest request) {
		errorList = new HashMap<String, String>();
		
		String parameterTaskName 		= request.getParameter(PortletsUtils.PARAMETER_TASK_NAME);
		String parameterTaskDescription = request.getParameter(PortletsUtils.PARAMETER_TASK_DESCRIPTION);
		
		if (parameterTaskName != null && parameterTaskName.trim().equals("")) {
			errorList.put(PortletsUtils.ERROR_PARAMETER_TASK_NAME, PortletsUtils.ERROR_PARAMETER_TASK_NAME);
		} else {
			temporaryTask.setName(parameterTaskName);
		}
		if (parameterTaskDescription != null && parameterTaskDescription.trim().equals("")) {			
			errorList.put(PortletsUtils.ERROR_PARAMETER_TASK_DESCRIPTION, PortletsUtils.ERROR_PARAMETER_TASK_DESCRIPTION);
		} else {
			temporaryTask.setDescription(parameterTaskDescription);			
		}
		
		temporaryTask.setPriority(request.getParameter(PortletsUtils.PARAMETER_TASK_PRIORITY));
		temporaryTask.setStatus(request.getParameter(PortletsUtils.PARAMETER_TASK_STATUS));	
	}
	
}
